/*******************************************************************************
Replication Materials for Blau, Kahn, Brummund, Cook, and Larson-Koester "Is 
There Still Son Preference in the United States?"

Table R1

Date Modified: 10/19/2019

*******************************************************************************/




*-------------------------------------------------------------------------------
                                   *LOAD DATA
*-------------------------------------------------------------------------------
use "$acs\acs_data", clear

*-------------------------------------------------------------------------------
                               *RESTRICT SAMPLE
*-------------------------------------------------------------------------------
keep if nonusbirth == 0 & father_sample != 1 & multi_sample != 1 & ///
	mult == 0 & female == 1 & oldc <=12 & inrange(age,18,40)==1  & marst == 1 ///
	& nchild >= 2
drop if citizen == 1

*-------------------------------------------------------------------------------
                          *GENERATE CORE AND EXTENDED SAMPLES
*-------------------------------------------------------------------------------
gen extended = 1
gen core = 0
replace core = 1 if nonrkids == 0 & nonusbirth == 0 & sfrelate == 0 & ///
	father_sample != 1 & multi_sample != 1 & mult == 0 & female == 1 & /// 
	oldc <=12 & inrange(age,18,40)==1 & foster_hh == 0

*-------------------------------------------------------------------------------
                  *GENERATE SAMPLES, VARIABLES AND WEIGHTS FOR EACH PANEL
*-------------------------------------------------------------------------------
// Spacing Variable

// Samples for each panel:
gen panelA = core == 1
gen panelB = core == 1 & bothfirmarr==1
gen panelC = extended == 1
gen panelD = extended == 1 & bothfirmarr==1

// WEights for each panel:
gen wgtA = hhwtnorm
gen wgtB = hhwtnorm
gen wgtC = perwtnorm
gen wgtD = perwtnorm

	
*-------------------------------------------------------------------------------
                                  *REGRESSIONS
*-------------------------------------------------------------------------------
label var chld1 "First Child Girl"	
local controls "lths scol cold sp_lths sp_scol sp_cold" //leave out: hsdi
local controls "`controls' i.genrace i.sp_genrace i.region i.year age age2 age3" 
local controls "`controls' sp_age sp_age2 sp_age3" 

// REGRESSION LOOP
*note: this loops over each panel and each dependent variable, a seperate
*table is generated for each panel
foreach p in A B C D  {

	// Main fertility results for the panel
		// NATIVE
		reg spacing chld1 `controls' if native == 1 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum spacing if native == 1 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R1_Panel_`p'", replace excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Native) keep(chld1) label nocons nor2 ///
		title("Table R1: Effects of a Female First Child on Birth Spacing - Panel `p'")

		// IMMIGRANTS
		reg spacing chld1 `controls' if native == 0 & panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum spacing if native == 0 & panel`p' == 1  [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R1_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Immigrant) keep(chld1) label nocons nor2

		// BOTH
		reg spacing chld1 `controls' if panel`p' == 1 [aw=wgt`p'],  robust
		return list
		local b = round(r(table)[1, 1], 0.0001)
		sum spacing if panel`p' == 1 [aw=wgt`p']
		local mean = round(r(mean), 0.0001)
		local mean: display %5.4f `mean'
		local prct: display %5.4f 100*`b'/`mean'
		outreg2 using "$routput\Table_R1_Panel_`p'", append excel dec(4) addtext(Dep. Var. Mean, `mean', Pct Effects, `prct') ctitle(nchild, Both) keep(chld1) label nocons nor2
	
}

